<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2024/8/8
 * Time: 16:21
 */

namespace App\Models\CacheModel\zl8;

use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

class Zl8AstrChatBusyRecord extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl8_astr_chat_busy_record';

    protected $cacheModel = 'voice_consult.reservation_busy';

    public function getAstrChatBusyList($astrId)
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $astrId)->getDataAndSet(function () use ($astrId)
        {
            try
            {
                return $this->where(['astr_id' => $astrId])->field('h_ids')->findOrFail()->toArray();
            }
            catch (\Exception $e)
            {
                return [];
            }
        });
        return $data ? explode(',', $data['h_ids']) : [];
    }

    public function saveData($data)
    {
        try
        {
            $id = $this->insertGetId(['astr_id' => $data['astr_id'], 'h_ids' => $data['h_ids'], 'create_time' => time()]);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $data['astr_id'])->rmCache();
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            $id = 0;
        }
        return $id;
    }

    public function updateData($astrId, $data)
    {
        try
        {
            $this->where('astr_id', $astrId)->update($data);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $astrId)->rmCache();
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
        return true;
    }
}
